﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// 用户操作类
/// </summary>
public class UserPage : System.Web.UI.Page
{

    private const string _defaultPass = "123456";
    private const string _roleSys = "sys_manage";

    public string RoleSys
    {
        get { return _roleSys; }
    }

    protected string LoginUser
    {
        get {
            UserValid();
            return (string)Session["LoginUser"];
        }
    }

    public static string DefaultPass
    {
        get { return UserPage._defaultPass; }
    }

	public UserPage() { }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        UserValid();
    }

    private void UserValid()
    {
        if (Session["LoginUser"] == null)
        {
            try
            {
                if (Tools.CheckAgent())
                {
                    Response.Redirect("~/MobileLogin.aspx", false);
                }
                else
                {
                    Response.Redirect("~/ULogin.aspx", false);
                }
            }
            catch (Exception ex)
            {
                log4net.LogManager.GetLogger(GetType()).Error("用户验证页面判断登录终端异常！" + ex.Message, ex);
            }
            finally
            {
                Response.End();
            }
        }
    }

    protected void JQueryAlert(string argMessage)
    {
        JQFunction("alert('" + argMessage + "');");
    }

    protected void JQFunction(string argFunction)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "sysinfo", "$(function(){" + argFunction + "});", true);
    }

    protected string GetPubDataName(string argType,string argCode)
    {
        string sqlstr = "SELECT DATA_NAME FROM u_pub_data_t WHERE DATA_TYPE = :DATA_TYPE AND DATA_CODE = :DATA_CODE";
        OracleParameter[] _params = {
                new OracleParameter(":DATA_TYPE",OracleType.VarChar,50),
                new OracleParameter(":DATA_CODE",OracleType.VarChar,20)
            };
        _params[0].Value = argType;
        _params[1].Value = argCode;
        return OracleHelper.ExecuteScalar(sqlstr, CommandType.Text, _params).ToString();
    }

    protected DataTable GetUser(string argLogin)
    {
        string sqlStr = @"SELECT login_name,real_name,if_valid,join_date,organ FROM u_user_info_t WHERE login_name=:login_name";
        OracleParameter[] _params = {
                new OracleParameter(":login_name",OracleType.VarChar,16)
            };
        _params[0].Value = argLogin;
        return OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, _params).Tables[0];
    }

    protected DataTable GetOrganAndCompany()
    {
        string sqlStr = @"select t1.real_name,t1.organ,t3.organ as company
                                  from u_user_info_t t1
                                  left join u_organ_t t2
                                    on t1.organ = t2.organ
                                  left join u_organ_t t3
                                    on t3.organ_type = 'organ_company'  
                                   and t2.lft between t3.lft and t3.rgt 
                                 where t1.login_name = :login_name
                                 order by t3.lft desc";
        OracleParameter[] _params = {
                new OracleParameter(":login_name",OracleType.VarChar,16)
            };
        _params[0].Value = LoginUser==null?"":LoginUser;
        return OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, _params).Tables[0];
    }
}
